//assign2.cpp

//***************************************************
//*												    *
//*	Student:		Jared Szechy	    		    *
//*	Login id:		cps350-N1.07			        *
//*	Assignment:		2								*
//*	Date:			02/13/08						*
//*													*
//*	This program demonstrates the use a binary		*
//*	tree.											*
//*													*
//***************************************************

#include <iostream>
#include <string>
#include <fstream>
#include "tree.h"
using namespace std;

void TestCopy(BinaryTree<int> tree, ostream &outs);
//pre: none
//post: tests the copy constructor

int main()
{
	//declarations
	string inputFile;                   //the name of the input file
	string outputFile = "szechy2.txt";  //the name of the output file  
	BinaryTree<int> treeArray[10];		//the array of trees
	ifstream ins;                       //the input stream
	ofstream outs;                      //the output stream
	string command;                     //the operation to be performed 
	int treeNum1, treeNum2;				//used to specify which tree	
	int pos, from;						//positions
	int item;							//the item being put into, deleted 
	string path;						//path to a node

	cout << "Please enter the name of the input file: ";
	cin >> inputFile;
	ins.open(inputFile.c_str());       //open input file
	outs.open(outputFile.c_str());     //open output file
	outs << "Jared Szechy" << endl;
	outs << "Input File: " << inputFile << endl << endl;


	//loop to process the input file line by line and call the correct
	//member function depending on the command word.
	while(ins >> command)
	{		
		ins >> treeNum1;
		if (command == "insert")
		{
			ins >> item;
			ins >> path;
			outs << "Command line: " << command << " " << treeNum1 << " " << item << endl;
			treeArray[treeNum1].Insert(item, path);
		}		
		else if (command == "printpre")
		{
			outs << "Command line: " << command << " " << treeNum1 << endl << '\t';
			treeArray[treeNum1].PrintPre(outs);
		}		
		else if (command == "printbottomtop")
		{
			outs << "Command line: " << command << " " << treeNum1 << endl << '\t';
			treeArray[treeNum1].PrintBottomToTop(outs);
		}
		else if (command == "assign")
		{
			ins >> treeNum2;
			outs << "Command line: " << command << " " << treeNum1 << " " << treeNum2 << endl;
			treeArray[treeNum1] = treeArray[treeNum2];
		}		
		else if (command == "deletesub")
		{
			ins >> item;
			outs << "Command line: " << command << " " << treeNum1 << " " << item << endl;
			if(treeArray[treeNum1].DeleteSubtreeAt(item))
				outs << '\t' << item << " was found in the tree and it's subtree was deleted." << endl;
			else
				outs << '\t' << item << " was not found in the tree." << endl;
		}
		else if (command == "sibling")
		{
			ins >> item;
			outs << "Command line: " << command << " " << treeNum1 << " " << item << endl;
			if(treeArray[treeNum1].SiblingOf(item, from))
				outs << '\t' << item << " has a sibling of " << from << endl;
			else
				outs << '\t' << item << " does not have a sibling or is not in the tree" << endl;
		}
		else if (command == "innext")
		{
			ins >> item;
			outs << "Command line: " << command << " " << treeNum1 << " " << item << endl;
			if (treeArray[treeNum1].InOrderNext(item, from))
				outs << '\t' << from << " is the next item after " << item << " in an in-order traversal" << endl;
			else
				outs << "\t" << item << " was not in the list or was the last item"	<< endl;
		}
		else if (command == "rightmost")
		{
			ins >> pos;
			outs << "Command line: " << command << " " << treeNum1 << " " << pos << endl;
			item = treeArray[treeNum1].RightmostOnLevel(pos);
			outs << "\tThe right most item on level " << pos << " is " << item << endl;
		}
		else if (command == "avgleafdepth")
		{
			outs << "Command line: " << command << " " << treeNum1 << endl;
			outs << "\tThe average leaf depth is " << treeArray[treeNum1].AvgLeafDepth() << endl;
		}
		else if (command == "testcopy")
		{
			outs << "Command line: " << command << " " << treeNum1 << endl;
			TestCopy(treeArray[treeNum1],outs);
		}
	}  
	ins.close();
	outs.close();
	cout << "Program Complete...\n";
	return 0;
}

void TestCopy(BinaryTree<int> tree, ostream &outs)
{
	outs << "  Copy\n------\n";
	tree.PrintPre(outs);
}